#include "IndexedPrimitiveNode.h"
#include "Irrlicht/ISceneManager.h"
#include "Irrlicht/IVideoDriver.h"

using namespace irr;


//
CIndexedPrimitiveNode::CIndexedPrimitiveNode(irr::scene::ISceneNode* parent,irr::scene::ISceneManager* mgr,
											 irr::s32 id,scene::SMeshBuffer* pMesh,irr::u32 primitiveCount) : scene::ISceneNode(parent,mgr,id),m_pMesh(pMesh),
											 m_primitiveCount(primitiveCount)
{
	pMesh->grab();
	m_primitiveType  = scene::EPT_TRIANGLES;   
}


//
CIndexedPrimitiveNode::~CIndexedPrimitiveNode()
{
	m_pMesh->drop();
}

//
void CIndexedPrimitiveNode::OnRegisterSceneNode()
{
	if (IsVisible) SceneManager->registerNodeForRendering(this);

	ISceneNode::OnRegisterSceneNode();
}

//
void CIndexedPrimitiveNode::render()
{
	if (!m_pMesh->getVertexCount() || !m_pMesh->getIndexCount()) return;


	video::IVideoDriver* driver = SceneManager->getVideoDriver();
	driver->setMaterial(m_pMesh->getMaterial());
	driver->setTransform(video::ETS_WORLD,AbsoluteTransformation);
	driver->drawVertexPrimitiveList(m_pMesh->getVertices(),m_pMesh->getVertexCount(),
		m_pMesh->getIndices(),m_primitiveCount,
		m_pMesh->getVertexType(),m_primitiveType,m_pMesh->getIndexType());   
}

//
const core::aabbox3d<f32>& CIndexedPrimitiveNode::getBoundingBox() const
{
	return m_pMesh->getBoundingBox();
}

//
irr::u32 CIndexedPrimitiveNode::getMaterialCount() const
{
	return 1;
}

//
irr::video::SMaterial& CIndexedPrimitiveNode::getMaterial(irr::u32 i)
{
	return m_pMesh->getMaterial();
}       


